package cn.jee.controller;

import cn.jee.entity.User;
import cn.jee.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class LoginController {
  @RequestMapping("/login_input")
  public String loginInput(){
    return "login";
  }
  @RequestMapping("/login")
  public String login(String username, String password, Model model, HttpSession session){
    System.out.println(username);
    model.addAttribute("username",username);
    session.setAttribute("login_user",username);
    return "login_suc";
  }
  @Autowired
  UserService service;
  @RequestMapping("/login_db")
  public String loginDb(User user, Model model, HttpSession session, RedirectAttributes redirectAttributes) {
    boolean isSuc = service.login(user);
    if (isSuc) {
      model.addAttribute("username", user.getUsername());
      session.setAttribute("login_user", user.getUsername());
      return "login_suc";
    } else {
      model.addAttribute("username","重定向看不到forward的值");
      redirectAttributes.addAttribute("redirect_msg","重定向传值");
      redirectAttributes.addFlashAttribute("flash_msg","只显示一次，刷新不见");
      return "redirect:/login_fail";//重定向必须指向action，不能是页面
    }
  }
  @RequestMapping("/login_fail")
  public String login_fail(){
    return "login_fail";
  }
}
